import { studentGetInfo, updateStudentInfo, uploadAvatar } from "../../api/manage";
import { baseUrl } from "../../api/request";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    showPanel: false,
    animationData: { translateY: '100%' },
    tempAvatar: "",
    name: "",
    grade: "",
    number: "",
    major: "",
    sex: "",
    phone: "",
    password: "",
    avatar: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('------------');
    const userID = wx.getStorageSync('userID');
    studentGetInfo(userID).then(res => {
      this.setData({
        name: res.data.studentName,
        number: res.data.studentNo,
        major: res.data.major,
        avatar: res.data.avatar,
        tempAvatar: res.data.avatar,
        grade: res.data.grade,
        sex: res.data.gender,
        phone: res.data.contact,
        password: res.data.password
      });
    });
  },

  // 点击编辑按钮，显示修改面板
  onEditClick: function () {
    this.showSlidePanel();
  },

  // 显示滑动面板
  showSlidePanel: function () {
    const animationData = {
      translateY: '0%'
    };
    this.setData({
      animationData: animationData,
      showPanel: true
    });
  },

  // 隐藏滑动面板
  hidePanel: function () {
    const animationData = {
      translateY: '100%'
    };
    this.setData({
      animationData: animationData,
    });
    setTimeout(() => {
      this.setData({
        showPanel: false
      });
    }, 300); // 确保动画完成后再隐藏面板
  },

  // 输入联系方式
  inputPhone: function (e) {
    this.setData({
      phone: e.detail.value
    });
  },

  // 输入密码
  inputPwd: function (e) {
    this.setData({
      password: e.detail.value
    });
  },

  // 选择头像
  onChooseAvatar: function () {
    wx.chooseMedia({
      count: 9,
      mediaType: ['image', 'video'],
      sourceType: ['album', 'camera'],
      maxDuration: 30,
      camera: 'back',
      success: (res) => {  // 使用箭头函数确保 `this` 指向正确
        console.log(res.tempFiles[0].tempFilePath)
        console.log(res.tempFiles[0].size)
        const avatarPath = res.tempFiles[0].tempFilePath
        console.log('oo', avatarPath);
        this.setData({
          avatar: avatarPath  // 这里的 `this` 就是指向页面实例
        });
      }
    })
  },
  

  // 确认修改
  confirmEdit: function () {
    const userID = wx.getStorageSync('userID');
    console.log('---rer',this.data.avatar,this.data.tempAvatar);

    // 如果头像发生变化
    if (this.data.tempAvatar != this.data.avatar) {
      wx.uploadFile({
        url: baseUrl + '/students/uploadAvatar', // 服务器上传接口地址
        filePath: this.data.avatar, // 本地头像路径
        name: 'file',  // 后端接收文件的字段名
        formData: {
          studentId: userID  // 传递学生ID
        },
        success: function (uploadRes) {
          const res = JSON.parse(uploadRes.data);  // 解析服务器响应
          wx.showToast({
            title: res.msg || '上传成功',
            icon: 'success'
          });
        },
        fail: function (error) {
          wx.showToast({
            title: '上传失败',
            icon: 'none'
          });
          console.log('上传头像失败:', error);
        }
      });
    }

    // 更新学生信息
    updateStudentInfo(userID, this.data.phone, this.data.password).then(res => {
      wx.showToast({
        title: res.msg,
        icon: 'none'
      });
      this.setData({
        showPanel: false
      });
    });
  }
});
